package com.mdd.admin.controller.notice;

import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.aop.Log;
import com.mdd.admin.service.ISystemNoticeService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.notice.SystemNoticeCreateValidate;
import com.mdd.admin.validate.notice.SystemNoticeValidate;
import com.mdd.admin.vo.notice.SystemNoticeDetailVo;
import com.mdd.admin.vo.notice.SystemNoticeListVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/adminapi/system/notice")
public class SystemNoticeController {

    @Resource
    ISystemNoticeService systemNoticeService;

    @GetMapping("/lists")
    @ApiOperation(value="系统操作日志")
    public AjaxResult<PageResult<SystemNoticeListVo>> lists(@Validated PageValidate pageValidate,
                                                            @Validated SystemNoticeValidate searchValidate) {
        PageResult<SystemNoticeListVo> list = systemNoticeService.pageList(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="文章详情")
    public AjaxResult<SystemNoticeDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        SystemNoticeDetailVo vo = systemNoticeService.detail(id);
        return AjaxResult.success(vo);
    }

    @Log(title = "文章新增")
    @PostMapping("/add")
    @ApiOperation(value="文章新增")
    public AjaxResult<Object> add(@Validated @RequestBody SystemNoticeCreateValidate createValidate) {
        Integer userId = LikeAdminThreadLocal.getAdminId();
        createValidate.setPublisherId(userId);
        systemNoticeService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "文章编辑")
    @PostMapping("/edit")
    @ApiOperation(value="文章编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody SystemNoticeCreateValidate updateValidate) {
        systemNoticeService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "文章删除")
    @PostMapping("/delete")
    @ApiOperation(value="文章删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        systemNoticeService.del(idValidate.getId());
        return AjaxResult.success();
    }

    @Log(title = "文章状态")
    @PostMapping("/updateStatus")
    @ApiOperation(value="文章状态")
    public AjaxResult<Object> change(@Validated @RequestBody IdValidate idValidate) {
        systemNoticeService.change(idValidate.getId());
        return AjaxResult.success();
    }

}
